<template>
  <div class="search-popup">
    <van-search
      v-model="searchVal"
      show-action
      :placeholder="placeholderVal"
      @search="onSearch"
      @cancel="onCancel"
      @input="onInput"
    />

    <HistoryHot
      :historyListData="historyListData"
      :hotKeywordListData="hotKeywordListData"
      v-if="blockShow == 1"
      @tagClick="tagClick"
    />

    <SearchTipsList
      v-else-if="blockShow == 2"
      :searchTipsListData="searchTipsListData"
      @ceilClick='tagClick'
    />

    <SearchProducts
      v-else
      :filterCategory="filterCategory"
      :goodsList="goodsList"
      @categoryChange="categoryChange"
      @priceChange="priceChange"
    />
  </div>
</template>

<script>
import HistoryHot from "@/components/HistoryHot";
import {
  getSearchPopupData,
  getSearchTipListData,
  getGoodsListData
} from "@/request/api";
import SearchTipsList from "@/components/SearchTipsList";
import SearchProducts from "@/components/SearchProducts";

export default {
  data() {
    return {
      searchVal: "", //搜索的文字，也就是用户输入的文本
      placeholderVal: "", //占位符文本
      blockShow: 1, //值可以为1或2或3
      //为1，表示展示历史记录和热门搜索
      //为2，表示展示搜索提示的列表
      //为3，表示展示搜索的产品的内容

      // 历史记录的列表数据
      historyListData: [],
      //热门搜索列表数据
      hotKeywordListData: [],
      //搜索实时提示的列表数据
      searchTipsListData: [],
      // 搜索产品分类数据
      filterCategory: [],
      // 搜索产品列表数据
      goodsList: [],
      //
      order: "asc",
      // 分类id
      categoryId: 0,
      // 默认的搜索方式是id还是price
      sort: "id"
    };
  },
  created() {
    getSearchPopupData().then(res => {
      if (res.errno == 0) {
        // console.log(res.data.hotKeywordList);
        this.placeholderVal = res.data.defaultKeyword.keyword;
        this.historyListData = res.data.historyKeywordList;
        this.hotKeywordListData = res.data.hotKeywordList;
      }
    });
  },
  methods: {
    tagClick(val) {
      //改变上面搜索框的值，后面再给价格排序的时候调用到priceChange的时候传入的就是这次点击的这个值
      this.searchVal = val;
      this.onSearch(val);
    },
    priceChange(value) {
      //value是desc或者asc
      this.order = value;
      this.sort = "price";
      this.onSearch(this.searchVal);
    },
    categoryChange(val) {
      // 改id
      this.categoryId = val;
      // 发送搜索商品的请求
      this.onSearch(this.searchVal);
      // console.log(this.searchVal);
    },
    onSearch(val) {
      //onSearch用户回车搜索的时候执行，val就是用户输入的值
      // console.log(val);
      this.blockShow = 3;

      getGoodsListData({
        keyword: val,
        page: 1,
        size: 10,
        order: this.order,
        categoryId: this.categoryId,
        sort: this.sort
      }).then(res => {
        if (res.errno == 0) {
          // console.log(res.data);

          let newArr = JSON.parse(
            JSON.stringify(res.data.filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          // console.log(newArr);
          this.filterCategory = newArr;
          this.goodsList = res.data.goodsList;
        }
      });
    },
    onCancel() {
      console.log("cancel");
      this.$router.go(-1);
      this.$store.commit("ChangeisShadow", false);
    },
    onInput(val) {
      this.blockShow = 2;
      //发送请求，获取实时搜索的文本提示的数据列表
      // console.log(val);
      getSearchTipListData({ keyword: val }).then(res => {
        if (res.errno == 0) {
          // console.log(res);
          this.searchTipsListData = res.data;
        }
      });
    }
  },
  components: {
    HistoryHot,
    SearchTipsList,
    SearchProducts
  }
};
</script>
 
<style lang = "less" scoped>
.search-popup {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  background-color: #efefef;
  z-index: 99;
}
</style>